public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.candy(new int[]{1,0,2}));
    }
}


class Solution {
    public int candy(int[] ratings) {
        int n = ratings.length;
        int[] left = new int[n], right = new int[n];
        left[0] = 1;
        right[n - 1] = 1;
        for (int i = 1; i < n; i++) {
            left[i] = ratings[i] > ratings[i - 1] ? left[i - 1] + 1 : 1;
            right[n - i - 1] = ratings[n - i - 1] > ratings[n - i] ? right[n - i] + 1 : 1;
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += Math.max(left[i], right[i]);
        }
        return sum;
    }
}